<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="120px">
                <el-form-item label="名称" prop="name">
                    <el-input v-model="formData.name" placeholder="请输入名称" clearable />
                </el-form-item>
                <el-form-item label="公众号appid" prop="appid">
                    <el-input v-model="formData.appid" placeholder="请输入公众号appid" clearable />
                </el-form-item>
                <el-form-item label="AppSecret" prop="link">
                    <el-input v-model="formData.app_secret" placeholder="请输入AppSecret" clearable />
                </el-form-item>
                <el-form-item prop="link">
                    <span style="color:red;font-weight:900;">
                        如何获取公众号appid和AppSecret
                    </span>
                    <el-button type="primary" link @click="dialogVisible = true">
                        查看
                    </el-button>
                </el-form-item>
                <el-form-item prop="link">
                    <span style="color:red;font-weight:900;">
                        登录微信公众平台去获取
                    </span>
                    <el-button type="primary" link @click="openwx">
                        立即登录
                    </el-button>
                </el-form-item>
            </el-form>
        </popup>
        <el-dialog
            v-model="dialogVisible"
            width="80%"
        >
            <img  src="../../assets/images/look.png"  alt="" style="width:100%;" @click="dialogVisible = false"/>
        </el-dialog>
    </div>
</template>
<script lang="ts" setup>
import Popup from '@/components/popup/index.vue'
import type { FormInstance } from 'element-plus'
import { editWechat,getWechatInfo,addWechat } from '@/api/application/gzh/index'
const dialogVisible = ref(false)
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑公众号' : '新增公众号'
})
const openwx = () => {
    window.open('https://mp.weixin.qq.com/')
}
const formData = reactive({
    name:"",
    app_secret:"",
    appid:""
})
const id = ref(0)
const handleClose = () => {
    emit('close')
}
const handleSubmit = async () => {
    await formRef.value?.validate()
    mode.value == 'edit' ? await editWechat({...formData,id:id.value}) : await addWechat({...formData})
    popupRef.value?.close()
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}
const setFormData = (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    id.value = row.id
    const data = await getWechatInfo({
        id: row.id
    })
    setFormData(data)
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
